

package Tool;

/**
 * 通过计算公式实现反正切、反正弦、反余弦<br>
 * Tan(X)=Sin(X)/Cos(x) <br>
 * ArcSin(X)=ArcTan(X/Sqrt(1-Sqr(X)))<br>
 * ArcCos(X)=ArcTan(Sqrt(1-Sqr(X))/X)<br>
 *
 * @author <a href="mailto:xzknet@gmail.com">Ken_xu</a>
 * @version 1.0 Copyright 2008-5-4 下午04:09:04
 */
public class MathExtend {
    /**
     * 反正切<br>
     * radian为正切值，这个函数的值域为－π/2～π/2
     *
     * @param radian
     *            弧度为单位的角度
     * @return 该角度所对应的反正切
     * @author Ken_xu
     */
    public static double arcTan(double radian) {
        System.out.println(radian);
        double retval = 0d;
        if (Math.abs(radian) <= 1.0d) {
            retval = radian / (1.0D + 0.28d * sqr(radian));
        } else {
            retval = -radian / (sqr(radian) + 0.28d);
            if (radian < -1.0d) {
                retval = retval - Math.PI / 2;
            } else {
                retval = retval + Math.PI / 2;
            }
        }
        return retval;
    }

    /**
     * 反正切<br>
     * 正切值为y/x，因此该函数求的是y/x所对应的角，这个函数的值域为－π～π
     *
     * @param y
     * @param x
     * @return 该角度所对应的反正切
     * @author Ken_xu
     */
    public static double arcTan(double y, double x) {
        if (y == 0.0d && x == 0.0d)
            return 0.0d;
        if (x > 0.0d)
            return arcTan(y / x);
        if (x < 0.0F)
            if (y < 0.0d)
                return (-(Math.PI - arcTan(y / x)));
            else
                return (Math.PI - arcTan(-y / x));
        return y >= 0.0F ? Math.PI / 2 : Math.PI / 2;
    }

    /**
     * 反正弦
     *
     * @param radian
     *            弧度为单位的角度
     * @return 该角度所对应的反正弦
     * @author Ken_xu
     */
    public static double arcSin(double radian) {
        // ArcSin(X)=ArcTan(X/Sqrt(1-Sqr(X)))
        double retval = arcTan(radian / Math.sqrt(1 - sqr(radian)));
        return retval;
    }

    /**
     * 反余弦
     *
     * @param radian
     *            弧度为单位的角度
     * @return 该角度所对应的反余弦
     * @author Ken_xu
     */
    public static double arcCos(double radian) {
        // ArcCos(X)=ArcTan(Sqrt(1-Sqr(X))/X)
        double retval = arcTan(Math.sqrt(1 - sqr(radian)) / radian);
        return retval;
    }

    /**
     * 平方函数
     *
     * @param x
     * @return 平方值( x * x )
     * @author Ken_xu
     */
    public static double sqr(double x) {
        return x * x;
    }
}





